<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>
        function request(option) {
            const {
                method = "get", url, params, success
            } = option

            let paramsStr = Object.entries(params).map(item => {

                return item.join("=")
            }).join("&")
            console.log(paramsStr)
           
            // 写请求
            const xhr = new XMLHttpRequest()
            xhr.open(method, url + "?" + paramsStr)
            xhr.send(null)

            xhr.onreadystatechange = function () {
                // 状态发生变化的时候打印状态
                // console.log(xhr.readyState, xhr.responseText)
                if (xhr.readyState === 4) {
                    // 数据成功接受了
                    if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
                        const res = xhr.responseText
                        // 这一步表示成功
                        success(res)
                    }
                }
            }
        }
        request({
            method: "get",
            url: "http://www.baidu.com",
            params: {
                name: "12",
                age: 16
            },
            success: function (data) {

            }
        })
        let p = new Promise(function (reslove, reject) {

            setTimeout(() => {
                console.log("定时器A执行")
                reslove()
            }, 1000)

        })
        p.then(function () {
                return new Promise(function (reslove, reject) {
                    setTimeout(() => {
                        console.log("定时器b执行")
                        reslove()
                    }, 1000)

                })

            })
            .then(function () {
                return new Promise(function (reslove, reject) {
                    setTimeout(() => {
                        console.log("定时器c执行")
                        reslove()
                    }, 1000)

                })

            })
            .then(function () {
                return new Promise(function (reslove, reject) {
                    setTimeout(() => {
                        console.log("定时器d执行")
                        reslove()
                    }, 1000)

                })

            })

        function wait(msg, delay) {
            return () => new Promise(function (reslove, reject) {
                setTimeout(() => {
                    console.log(msg)
                    reslove()
                }, 1000)
            })
        }
        Promise.resolve()
            .then(wait("A执行", 1000))
            .then(wait("B执行", 1000))
            .then(wait("C执行", 1000))

        function requestPromise(options) {
            const {
                method,
                url,
                params,
                success
            } = options

            return new Promise((res, rej) => {
                request({
                    method,
                    url,
                    params,
                    success: res
                })
            })
            console.log(success)
        }
        requestPromise({
                method: "get",
                url: "url",
                params: {
                    a: 1
                }
            })
            .then((response) => {
                console.log(response)
            })
    </script>




</body>

</html>